package org.haifi.game.util;

import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

/**
 * 将excel解析为java对象
 * 
 * 要求excel中全部为文本格式
 * 
 * 第一列是属性名
 * 
 * 第二列是备注
 * 
 * 从第三列开始，所有第一行非空的为正式数据
 * 
 * @author joycube2
 * 
 */
public class XlsUtil {
	public static List parse(String xlsName, String sheetName, Class clazz) {
		List list = Lists.newArrayList();
		try {

			// TODO:这里有可能需要改为使用resources的加载方式
			InputStream inputStream = Thread.currentThread()
					.getContextClassLoader().getResourceAsStream(
							"xls/" + xlsName);
			HSSFWorkbook workbook = new HSSFWorkbook(inputStream);

			HSSFSheet sheet = workbook.getSheet(sheetName);

			HSSFRow row1 = sheet.getRow(0);
			HSSFRow row2 = sheet.getRow(1);

			int rows = sheet.getLastRowNum();

			for (int m = 2; m < rows; m++) {
				HSSFRow row = sheet.getRow(m);
				// 取第一个单元格，如果为空则跳过
				if (StringUtils.isEmpty(parseCellText(row, 0))) {
					continue;
				}

				Object object = clazz.newInstance();

				String[] props = parseCell(row1);
				for (int n = 0; n < props.length; n++) {
					String text = parseCellText(row, n);
					setProp(object, props[n], text);
				}
			}

		} catch (Exception e) {
			log.error("ERROR", e);
		}
		return list;
	}

	/**
	 * 目前只用于过滤字符用
	 * 
	 * @param xlsName
	 * @param sheetName
	 * @return
	 */
	public static Set<String> parse(String xlsName, String sheetName) {
		Set<String> results = Sets.newHashSet();
		try {
			InputStream inputStream = Thread.currentThread()
					.getContextClassLoader().getResourceAsStream(
							"xls/" + xlsName);
			POIFSFileSystem fileSystem = new POIFSFileSystem(inputStream);
			HSSFWorkbook workbook = new HSSFWorkbook(fileSystem);
			HSSFSheet sheet = workbook.getSheet(sheetName);
			for (int m = 0; m <= sheet.getLastRowNum(); m++) {
				HSSFRow row = sheet.getRow(m);
				String text = parseCellText(row, 0);
				if (!StringUtils.isEmpty(text)) {
					results.add(text);
				} else {
					log.warn("meet empty string with xls:" + xlsName
							+ "\tsheet:" + sheetName + "\trow:" + m);
				}
			}
		} catch (Exception e) {
			log.error("ERROR", e);
		}
		return results;
	}

	/**
	 * 设置属性值
	 * 
	 * @param obj
	 * @param prop
	 * @param value
	 */
	public static void setProp(Object obj, String prop, String value)
			throws Exception {
		Class clazz = obj.getClass();
		Field filed = clazz.getField(prop);
		if (filed.getType() == Integer.class) {

		}
	}

	public static String[] parseCell(HSSFRow row) {
		String[] results = new String[row.getLastCellNum()];
		for (int m = 0; m < results.length; m++) {
			results[m] = parseCellText(row, m);
		}
		return results;
	}

	public static String parseCellText(HSSFRow row, int cellNum) {
		String text = "";
		HSSFCell cell = row.getCell(cellNum);
		text = cell.getStringCellValue();
		return text;
	}

	private static final Log log = LogFactory.getLog(XlsUtil.class);
}
